package com.codesuc.aichat.common;

import com.codesuc.aichat.constant.CommonConstant;
import com.codesuc.aichat.enums.CodeSucErrorEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * @Description 统一结果响应类
 * @Author Mr.Zhang
 * @Date 2025/8/3 15:36
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@Schema(name = "统一响应返回结果对象")
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;


    /**
     * 成功标志
     */
    @Schema(name = "成功标志")
    private boolean success = true;

    /**
     * 返回处理消息
     */
    @Schema(name = "返回处理消息")
    private String message = "";

    /**
     * 返回代码
     */
    @Schema(name = "返回代码")
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    @Schema(name = "返回数据对象")
    private T data;

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> ok() {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        return r;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> ok(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        r.setData(null);
        return r;
    }

    public static <T> Result<T> ok(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> error(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
        r.setMessage(msg);
        r.setData(data);
        return r;
    }

    public static <T> Result<T> error(String msg) {
        return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

    public static <T> Result<T> error(int code, String msg) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    /**
     * 无权限访问返回结果
     */
    public static <T> Result<T> noAuth(String msg) {
        return error(CommonConstant.SC_NO_AUTHZ, msg);
    }

    /**
     * 构建错误结果
     *
     * @param codeEnum 错误枚举
     * @return 错误结果
     * @param <T> 泛型
     */
    public static <T> Result<T> buildResult(CodeSucErrorEnum codeEnum) {
        return Result.error(codeEnum.getCode(), codeEnum.getMessage());
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = CommonConstant.SC_OK_200;
        this.success = true;
        return this;
    }

    public <T> Result<T> success(String message, T data) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setMessage(message);
        r.setCode(CommonConstant.SC_OK_200);
        r.setData(data);
        return r;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
        return this;
    }
}
